package org.zdc.pivot.wtk;
import haxe.Json;
import nme.geom.Point;
import nme.geom.Rectangle;
import org.zdc.pivot.core.ISerializable;
import org.zdc.pivot.error.IllegalArgumentException;

class Bounds extends Rectangle, implements ISerializable
{
	private static inline var serialVersionUID = -2473226417628417475;

	public static inline var X_KEY:String = "x";
	public static inline var Y_KEY:String = "y";
	public static inline var WIDTH_KEY:String = "width";
	public static inline var HEIGHT_KEY:String = "height";
	
	public function new(x:Int, y:Int, width:Int, height:Int) 
	{
		super();
		
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
	}
	
	public static function make(orign:Dynamic):Bounds {
		var x = Reflect.getProperty(orign, X_KEY);
		var y = Reflect.getProperty(orign, y_KEY);
		var w = Reflect.getProperty(orign, WIDTH_KEY);
		var h = Reflect.getProperty(orign, HEIGHT_KEY);
		if (x == null || y == null || w == null || h == null) {
			return null;
		}
		return new Bounds(x, y, w, h);
	}
	
	public function translate(dx:Int, dy:Int):Bounds {
		return new Bounds(x + dx, y + dy, width, height);
	}
	
	public function equals(object:Dynamic):Bool {
		var eq = false;
		if(Std.is(object, Bounds)) {
			eq = (	object.x == x && 
					object.y == y &&
					object.width == width &&
					object.height == height );
		}
		return eq;
	}
	
	public function hashCode() {
		var prime = 33;
		var result = 1;
		result = prime * result + height;
		result = prime * result + width;
		result = prime * result + x;
		result = prime * result + y;
		return result;
	}
	
	public static function decode(value:String):Bounds {
		if (value == null) {
			throw new IllegalArgumentException();
		}
		
		try {
			var json = Json.parse(value);
			return new Bounds(json.x, json.y, json.width, json.height);
		}
	}
	
	public function isEmpty():Bool {
		return (width == 0 || height == 0);
	}
	
	
}